﻿Module ExtendedStringMethods

  Public Function StringSplitByString(ByVal src As String, ByVal sep As String) As String()
    Dim ret As New List(Of String)
    Dim srcc As New String(src)

    While srcc <> String.Empty
      Dim idx As Integer = srcc.IndexOf(sep)
      If idx >= 1 Then
        ret.Add(srcc.Substring(0, idx))
        If idx + sep.Length < srcc.Length Then
          srcc = srcc.Substring(idx + sep.Length)
        Else
          srcc = String.Empty
        End If
      Else
        ret.Add(srcc)
        srcc = String.Empty
      End If
    End While

    Return ret.ToArray
  End Function

  Public Function StringCaseInsensitiveReplace(ByVal original As String, ByVal search As String, ByVal replace As String) As String
    If String.IsNullOrEmpty(original) OrElse String.IsNullOrEmpty(search) Then
      Return original
    End If

    Dim idxSearch As Integer = -1
    Dim idxLast As Integer = 0
    Dim result As New System.Text.StringBuilder

    While True
      idxSearch = original.IndexOf(search, idxSearch + 1, StringComparison.OrdinalIgnoreCase)

      If idxSearch < 0 Then
        result.Append(original, idxLast, original.Length - idxLast)
        Exit While
      End If

      result.Append(original, idxLast, idxSearch - idxLast)
      result.Append(replace)
      idxLast = idxSearch + search.Length
    End While

    Return result.ToString
  End Function

  Public Function StringRemoveBetween(ByVal original As String, ByVal startStr As String, ByVal endStr As String) As String
    If String.IsNullOrEmpty(original) OrElse String.IsNullOrEmpty(startStr) OrElse String.IsNullOrEmpty(endStr) Then
      Return original
    End If

    Dim idxStart As Integer = -1
    Dim idxEnd As Integer = -1
    Dim idxLast As Integer = 0
    Dim result As New System.Text.StringBuilder

    While True
      idxStart = original.IndexOf(startStr, idxStart + 1, StringComparison.OrdinalIgnoreCase)
      idxEnd = original.IndexOf(endStr, idxStart + 1, StringComparison.OrdinalIgnoreCase)

      If idxStart < 0 OrElse idxEnd < 0 Then
        result.Append(original, idxLast, original.Length - idxLast)
        Exit While
      End If

      result.Append(original, idxLast, idxStart - idxLast)
      idxLast = idxEnd + endStr.Length
    End While

    Return result.ToString
  End Function

  Public Function StringNoNull(ByVal str As String)
    If str Is Nothing Then
      Return String.Empty
    Else
      Return str
    End If
  End Function

  Public Function StripExtraSlashesInPath(ByVal str As String) As String
    Dim path As String = str

    While path.Contains("\\")
      path = path.Replace("\\", "\")
    End While

    Return path
  End Function

  Public Function GetRandomFileName(ByVal folder As String, ByVal seed As String, ByVal randLen As Integer, ByVal countLen As Integer, ByVal ext As String) As String
    Const validChars As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789_"
    Dim i As Integer = 0
    Dim path As String = String.Empty

    Dim seedc() As Char = seed.ToArray
    For i = 0 To seedc.Length - 1
      If Not validChars.Contains(seedc(i)) Then
        seedc(i) = "_"
      End If
    Next
    seed = New String(seedc)

    i = 0
    Do
      Dim g As Guid = Guid.NewGuid()
      path = seed & Right(g.ToString("N"), randLen) & i.ToString("D" & countLen.ToString)
      path = folder & "\" & path & ext
      path = StripExtraSlashesInPath(path)
      i += 1
    Loop While IO.File.Exists(path)

    Return path
  End Function

End Module
